package com.example.bailin.giftsay.tool;

import android.os.Handler;
import android.os.Looper;

import com.example.bailin.giftsay.entity.GuanZhuEntity;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.assit.QueryBuilder;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by bailin on 16/8/24.
 * <p/>
 * 数据库操作类
 * 使用单例模式，因为LiteOrm要求的
 * 其他数据库框架 GreenDao, QrmLite
 */
public class DBTool {

    private static DBTool ourInstance = new DBTool();
    private LiteOrm mLiseOrm;
    private Handler mHandler;

    ExecutorService threadPool = Executors.newFixedThreadPool(5);


    public static DBTool getInstance() {
        return ourInstance;
    }

    private DBTool() {
        mLiseOrm = LiteOrm.newSingleInstance(MyApp.getContext(), "GiftSay.db");
        mHandler = new Handler(Looper.getMainLooper());
    }

    public void insertEntity(final GuanZhuEntity guanZhuEntity) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                mLiseOrm.insert(guanZhuEntity);
            }
        });
    }

    public void queryGuanZhu(final String where, final String where2, final String str, final String str2, final QueryListener queryListener) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                QueryBuilder<GuanZhuEntity> builder = new QueryBuilder<GuanZhuEntity>(GuanZhuEntity.class);
                builder.where(where, str);
                builder.whereAppendAnd().whereAppend(where2, str2);
                ArrayList<GuanZhuEntity> guanZhuEntities = mLiseOrm.query(builder);
                mHandler.post(new HandlerRunnable(guanZhuEntities, queryListener));
            }
        });
    }

    public void queryUserName(final String where, String where2, final String userName, final String str, final QueryListener queryListener) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                QueryBuilder<GuanZhuEntity> builder = new QueryBuilder<GuanZhuEntity>(GuanZhuEntity.class);
                builder.where(where, userName);
                builder.whereAppendAnd().whereAppend(where, str);
                ArrayList<GuanZhuEntity> guanZhuEntities = mLiseOrm.query(builder);
                mHandler.post(new HandlerRunnable(guanZhuEntities, queryListener));
            }
        });
    }

    public void deleteGuanZhu(final String where, final String name, final String str, final String str2) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                QueryBuilder<GuanZhuEntity> builder = new QueryBuilder<GuanZhuEntity>(GuanZhuEntity.class);
                builder.where(where, str);
                builder.whereAppendAnd().whereAppend(name, str2);
                ArrayList<GuanZhuEntity> guanZhuEntities = mLiseOrm.query(builder);
                mLiseOrm.delete(guanZhuEntities);
            }
        });
    }

    class HandlerRunnable implements Runnable {
        ArrayList<GuanZhuEntity> mGuanZhu;
        QueryListener mQueryListener;

        public HandlerRunnable(ArrayList<GuanZhuEntity> guanZhuEntities, QueryListener queryListener) {
            this.mGuanZhu = guanZhuEntities;
            this.mQueryListener = queryListener;
        }

        @Override
        public void run() {
            mQueryListener.onQueryComplete(mGuanZhu);
        }
    }

    public interface QueryListener {
        void onQueryComplete(ArrayList<GuanZhuEntity> guanZhuEntities);
    }

}
